<?php


namespace app\common\cachemodel\zl1_relation;


use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

class Zl1RelationText extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl1_relation_text';

    protected $cacheModel = 'astr_community.relation_text';

    /**
     * 获取星座关系介绍词条
     * @param int $inSign 星座
     * @return array[]
     * @throws \Exception
     */
    public function getRelationTextList(int $inSign)
    {
        $ids = $this->getRelationTextIds($inSign);

        $dataList = $this->batchGetInfoByIds(array_keys($ids));

        $resData = [1 => [], 2 => []];
        foreach ($dataList as $item)
        {
            $resData[$item['type']][] = [
                'id' => $item['id'],
                'title' => $item['title'],
                'avatar' => $item['avatar'],
                'tags' => explode('，', $item['keywords']),
                'content' => $item['text'],
                'in_sign' => $item['in_sign'],
                'relation_id' => $item['tid'],
                'tip' => $item['recommend_text'],
                'tags_color' => $item['keywords_color'],
                'tags_bg_color' => $item['keywords_base_color'],
            ];
        }

        return $resData;
    }

    /**
     * 获取星座关系介绍词条IDS
     * @param int $inSign
     * @return array
     * @throws \Exception
     */
    public function getRelationTextIds(int $inSign)
    {
        return CacheManage::getInstance()->setCacheConfig($this->cacheModel, $inSign)->setSort('DESC')->ZsGetIocData('list', function () use ($inSign)
        {
            $where = ['in_sign' => $inSign, 'status' => 1];
            try
            {
                $data = $this->where($where)->order('sort desc')->column('sort', 'id');
                if (!$data)
                {
                    $data = [0 => 0];
                }
            }
            catch (\Exception $e)
            {
                $data = [0 => 0];
            }
            return $data;
        }, true);
    }


    /**
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        $where = ['t.id' => ['in', $ids]];
        $field = 'tag.type,t.status,t.tid,t.sort,t.in_sign,t.title,t.avatar,t.keywords,t.keywords_color,t.keywords_base_color,t.text,t.recommend_text,t.create_time';
        try
        {
            $data = $this->alias('t')
                ->join('wechat_backup_07_18_14_43_00.zl1_relation_tag tag', 't.tid=tag.id')
                ->field($field)
                ->where($where)
                ->column($field, 't.id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        foreach ($resData as $orderId => $orderItemData)
        {
            $resData[$orderId] = arrayKeyData2int($orderItemData, ['id', 'in_sign', 'sort', 'status', 'type', 'tid']);
        }

        return $resData;
    }
}
